package com.yunfeisoft.business.service.impl;

import com.applet.base.BaseServiceImpl;
import com.applet.sql.separation.DataSourceChange;
import com.applet.utils.Page;
import com.yunfeisoft.business.dao.inter.CmsSubjectDao;
import com.yunfeisoft.business.dao.inter.CmsSubjectProductRelationDao;
import com.yunfeisoft.business.model.CmsSubject;
import com.yunfeisoft.business.service.inter.CmsSubjectService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * ClassName: CmsSubjectServiceImpl
 * Description: 专题表service实现
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Service("cmsSubjectService")
public class CmsSubjectServiceImpl extends BaseServiceImpl<CmsSubject, String, CmsSubjectDao> implements CmsSubjectService {

    @Autowired
    private CmsSubjectProductRelationDao cmsSubjectProductRelationDao;

    @Override
    @DataSourceChange(slave = true)
    public Page<CmsSubject> queryPage(Map<String, Object> params) {
        return getDao().queryPage(params);
    }

    @Override
    public int modifyShowStatus(String id, int showStatus) {
        if (StringUtils.isBlank(id)) {
            return 0;
        }

        CmsSubject subject = new CmsSubject();
        subject.setId(id);
        subject.setShowStatus(showStatus);
        return getDao().update(subject);
    }

    @Override
    public int modifyRecommendStatus(String id, int recommendStatus) {
        if (StringUtils.isBlank(id)) {
            return 0;
        }

        CmsSubject subject = new CmsSubject();
        subject.setId(id);
        subject.setRecommendStatus(recommendStatus);
        return getDao().update(subject);
    }

    @Override
    public int save(CmsSubject cmsSubject) {
        if (CollectionUtils.isNotEmpty(cmsSubject.getSubjectProductList())) {
            cmsSubjectProductRelationDao.batchInsert(cmsSubject.getSubjectProductList());
        }
        return super.save(cmsSubject);
    }

    @Override
    public int modify(CmsSubject cmsSubject) {
        cmsSubjectProductRelationDao.removeBySubjectId(cmsSubject.getId());
        if (CollectionUtils.isNotEmpty(cmsSubject.getSubjectProductList())) {
            cmsSubjectProductRelationDao.batchInsert(cmsSubject.getSubjectProductList());
        }
        return super.modify(cmsSubject);
    }
}